package wli.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import wli.hibernate.db.Spelling;
import wli.hibernate.db.Subject;
import wli.hibernate.db.Vocabulary;
import wli.hibernate.util.HibernateUtil;
import wli.web.util.DateAndTimeUtil;
import wli.web.util.HtmlUtil;
import wli.web.util.MyDateAndTime;

/**
 * Servlet implementation class SpellingBee
 */
@WebServlet("/SpellingBee")
public class SpellingBeeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public SpellingBeeServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			PrintWriter out = response.getWriter();
			HttpSession session = request.getSession(true);
			Session hsession = null;
			Session hsession1 = null;
			Transaction transaction = null;
			Subject subj = null;

			Vector vdate = new Vector();
			vdate.add(DateAndTimeUtil.getCurrentDate(3));
			List vlist1 = new ArrayList();
			hsession1 = HibernateUtil.getSessionFactory().openSession();
			Criteria criteria1 = (Criteria) hsession1
					.createCriteria(Spelling.class);

			vlist1 = criteria1.list();

			// System.out.println("<h3>DDDDDDDDDDDDDDDDDDDDDDDDDDDD============"+vlist1+"=============DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD</h3>");

			List vlist = new ArrayList();

			hsession = HibernateUtil.getSessionFactory().openSession();
			Criteria criteria = hsession.createCriteria(Vocabulary.class);
			// vlist =
			// criteria.add(Restrictions.isNotEmpty("Vocabulary")).list();
			vlist = criteria.list();
			// Collections.sort(vlist);
			Vocabulary vc = null;
			Iterator it = vlist.iterator();

			Spelling spb = null;
			Iterator it1 = vlist1.iterator();
			String temp = null;
			while (it1.hasNext()) {
				spb = (Spelling) it1.next();
				temp = spb.getSchedule().toString().substring(0, 10);
				// out.println("<h3>"+spb.getSchedule()+"</h3>");
				if (!vdate.contains(temp)) {
					vdate.add(temp);
				}
			}
			
			out.println("<h3>" + vdate + "</h3>");

			out.println("<table><tr><td>");
			out.println("<table border=1>");
			while (it.hasNext()) {
				vc = (Vocabulary) it.next();
				out.println("<tr><td><a href=?word=" + vc.getWord() + ">"
						+ vc.getWord() + "</a></td>");
				out.println("<td><a href=?action=add&word=" + vc.getWord()
						+ ">Add</a></td></tr>");
			}

			out.println("</table>");
			out.println("</td><td valign=top>");
			String action = request.getParameter("action");
			if (action != null && action.equals("add")) {
				out.println("<FORM>");
				out.println("<table border=0 width=1000 bgcolor=#336699><tr>");
				out.println("<td bgcolor=#ffffff style=font-size:20px><b>"
						+ request.getParameter("word") + "</b></td>");
				out.println("<td>defination:</td><td><input type=text name=defination size=100></td>");
				out.println("<td>Add To:</td><td>"
						+ HtmlUtil.getSelect(DateAndTimeUtil.getCurrentDate(3),
								"schedule", vdate, null) + "</td>");
				out.println("<td><input type=submit value=Add></td>");
				out.println("</tr></table>");
				out.println("</FORM>");
			}
			out.println("<table><tr><td valign=top><IFRAME src=http://dictionary.reference.com/browse/"
					+ request.getParameter("word")
					+ " width=1000 height=800></iframe></td></tr></table>");

			out.println("</td></tr></table>");

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
